<?php

declare(strict_types=1);

namespace app\admin\service;

use app\lib\exception\Forbidden;
use app\lib\exception\TokenEXception;

class Token
{
    private static $token = '';
    public static function checkPrimaryScope(string $token): int
    {
        self::$token = $token;
        $scope = self::getCurrentTokenVar('scope');
        if ($scope) {
            if ($scope >= config('scope.User')) {
                return self::getCurrentTokenVar('uid');
            } else {
                throw new Forbidden();
            }
        } else {
            throw new TokenEXception();
        }
    }
    public static function getCurrentTokenVar(string $key)
    {
        $vars = cache(self::$token);
        if (!$vars) {
            throw new TokenEXception();
        } else {
            if (!is_array($vars)) {
                $vars = json_decode($vars, true);
            }

            if (array_key_exists($key, $vars)) {
                return $vars[$key];
            } else {
                throw new TokenEXception(['msg' => '权限查询失败']);
            }
        }
    }
}
